package com.soft.idc.module.local;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.jetty.util.log.Log;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.CaptchaRender;
import com.soft.idc.common.control.AuthInterceptor;
import com.soft.idc.common.model.IdcOrg;
import com.soft.idc.common.model.IdcRole;
import com.soft.idc.common.model.IdcUser;
import com.soft.idc.common.util.JQList;
import com.soft.idc.common.util.JQPage;

@Before(AuthInterceptor.class)
public class LocalMonitorController extends Controller {

	public void index() {

		this.renderJsp("/local/main.jsp");
	}

	public void loadIdcInfo() {
		// IdcUser u = (IdcUser) this.getSessionAttr("SYS_USER");
		String sql = "select (select code from idc_kpi_define d where d.kpi_id=i.kpi_id) as code,"
				+ "i.value from idc_kpi_record_ing i where i.obj_id = ? "
				+ "and kpi_id in (100042,100047,100048,100054,100055,100161,100162);";
		List<Record> appServer = Db.find(sql, 100125);
		List<Record> dbServer = Db.find(sql, 100126);

		Map<String, String> appMap = new HashMap<String, String>();
		for (Record vo : appServer) {
			appMap.put(vo.getStr("code"), vo.getStr("value"));
		}

		Map<String, String> dbMap = new HashMap<String, String>();
		for (Record vo : dbServer) {
			dbMap.put(vo.getStr("code"), vo.getStr("value"));
		}

		this.setAttr("appMap", appMap);
		this.setAttr("dbMap", dbMap);
		this.renderJson();
	}

	public void loadKpi() {
		String sql = "select date_format(create_time,'%Y-%m-%d %H:%i:00') as create_time,value from idc_kpi_record where obj_id = ? and kpi_id = ? "
				+ "and create_time >= str_to_date(?,'%Y-%m-%d %H:%i:%s') order by create_time asc";
		String objId = this.getPara("objId");
		String kpiId = this.getPara("kpiId");
		String createTime = this.getPara("createTime");
		List<Record> kpis = Db.find(sql, objId, kpiId, createTime);

		this.renderJson(kpis);
	}

	public void loadDisk() {
		int objId = this.getParaToInt("deviceId");
		int kpiId = this.getParaToInt("kpiId");

		String sql = "select obj_id,kpi_id,(select code from idc_kpi_define kd where kd.kpi_id =k.kpi_id ) kpi_code,(select name from idc_device d where d.device_id = k.obj_id) obj_name,value,create_time from idc_kpi_record_ing k  "
				+ " where  obj_type = 0 and obj_id = ? and kpi_id = ?";
		Record vo = Db.findFirst(sql, objId, kpiId);
		List list = new ArrayList();
		if (vo != null) {
			String value = vo.getStr("value");
		//	if (value.indexOf("|") != -1) {
				String[] str = value.split("\\|");
				for (String s : str) {
					String[] params = s.split("\\,");
					Map map = new HashMap();
					map.put("path", params[0]);
					map.put("rate", params[1]);
					map.put("used", params[2]);
					map.put("free", params[3]);
					list.add(map);

				}
	//		}

		}
		this.renderJson(new JQList(list));
	}

}
